package com.example.tests;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import java.io.FileReader;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;

import com.example.fw.ApplicationManager;
import com.example.fw.ContactObject;
import com.example.fw.GroupObject;
import com.example.fw.Groups;

public class TestBase {
	protected ApplicationManager app;

	protected Logger log = Logger.getLogger("TEST");



	@BeforeClass
	@Parameters({"configFile"})
	public void setUp(@Optional String configFile) throws Exception {
		if(configFile == null){
			configFile = System.getProperty("configFile");
		}
		if(configFile == null){
			configFile = System.getenv("configFile");
		}
		if(configFile == null){
			configFile = "application.properties";
		}
		Properties props = new Properties();
		props.load(new FileReader(configFile));
		log.info("setUp start");
		app = ApplicationManager.getInstance();
		app.setProperties(props);
		log.info("setUp end");
	}

	@AfterTest
	public void tearDown() throws Exception {
		log.info("tearDown start");
		ApplicationManager.getInstance().stop();
		log.info("tearDown end");
	}

//	public void verifyGroupAdded(Set<GroupObject> oldList,
//			GroupObject validGroup, Set<GroupObject> newList) {
//		assertEquals(newList.size(), oldList.size()+1);
//		assertThat(newList.size(), equalTo(oldList.size()+1));
//		validGroup.setId("$$$");
//		oldList.add(validGroup);
//		validGroup.setId(null);
//		assertThat(newList, equalTo(oldList));
		

//	}	

	public void verifyGroupModified(Groups oldList,
			GroupObject validGroup, Groups newList) {
	//	assertThat(newList.size(), equalTo(oldList.size()));
		//assertThat(newList, equalTo(oldList));
	}

	public void verifyGroupdeleted(Set<GroupObject> oldList,
			Set<GroupObject> newList) {
		assertThat(newList.size(), equalTo(oldList.size()-1));
	}

	public  void verifyContactAdded(Set<ContactObject> oldList,
			ContactObject validContact, Set<ContactObject> newList) {
		assertThat(newList.size(), equalTo(oldList.size()+1));
		validContact.setId("$$$");
		oldList.add(validContact);
		validContact.setId(null);
		assertThat(newList, equalTo(oldList));
	}
	public  void verifyContactModified(Set<ContactObject> oldList,
			ContactObject validContact, Set<ContactObject> newList) {
		assertThat(newList.size(), equalTo(oldList.size()));
	}
	public  void verifyContactDeleted(Set<ContactObject> oldList,
			 Set<ContactObject> newList) {
		assertThat(newList.size(), equalTo(oldList.size()-1));
		}
}
